
#include <BNode>
#include <BShape>
#include <BRender>
#include <BMouseEvent>
#include <BViewer>
#include <BPickItem>

#include "MyHandler.h"

MyHandler::MyHandler()
{
	_target = 0;
	_hoverObject = 0;
}
MyHandler::~MyHandler()
{

}

void MyHandler::setTarget(BObject* target)
{
	_target = target;
	_hoverObject = 0;
	this->fresh();
}
BObject* MyHandler::target() const
{
	return _target;
}

void MyHandler::paint(BPainter& painter)
{

}
void MyHandler::render(BRender& render)
{
	if (_target)
	{
		BBox box;
		if (BShape* shape = dynamic_cast<BShape*>(_target))
		{
			box = shape->box();
		}
		if (BNode* node = dynamic_cast<BNode*>(_target))
		{
			box = node->box();
		}
		render.setLighting(false);
		render.drawBox(box);
	}
	if (_hoverObject)
	{
		BBox box;
		if (BShape* shape = dynamic_cast<BShape*>(_hoverObject))
		{
			box = shape->box();
			render.setColor(100, 255, 0);
		}
		if (BNode* node = dynamic_cast<BNode*>(_hoverObject))
		{
			box = node->box();
			render.setColor(255, 100, 0);
		}
		render.setLighting(false);
		render.drawBox(box);
	}
}

bool MyHandler::keyDownEvent(const BKeyEvent& keyEvent)
{
	return false;
}
bool MyHandler::keyUpEvent(const BKeyEvent& keyEvent)
{
	return false;
}

bool MyHandler::mousePressEvent(const BMouseEvent& mouseEvent)
{
	BSphereHandler::mousePressEvent(mouseEvent);
	if (mouseEvent.button() == Button_Left)
	{
		_target = _hoverObject;
		this->fresh();
	}
	return false;
}
bool MyHandler::mouseReleaseEvent(const BMouseEvent& mouseEvent)
{
	BSphereHandler::mouseReleaseEvent(mouseEvent);
	return false;
}
bool MyHandler::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	BSphereHandler::mouseMoveEvent(mouseEvent);
	if (mouseEvent.button() == Button_None)
	{
		const BPoint& pos = mouseEvent.pos();
		BObject* object = 0;
		const BPickItem* item = this->viewer()->pick(pos);
		if (item)
		{
			object = item->node();
			if (!object)
				object = item->shape();
		}
		if (_hoverObject != object)
		{
			_hoverObject = object;
			this->fresh();
			return true;
		}
	}
	return false;
}

